package com.jy.api.service.impl;

import com.jy.api.dao.TeamDao;
import com.jy.api.domain.Team;
import com.jy.api.service.TeamService;
import com.jy.api.signUpReq.team.AddTeamReq;
import com.jy.api.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xiaohua on 2018/12/24.
 */
@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    private TeamDao teamDao;


    @Override
    @Transactional
    public Result addNewTeam(AddTeamReq req) {
        Team team = teamDao.findByName(req.getName());
        if (team == null) {
            team = new Team();
            team.setName(req.getName());
            teamDao.save(team);
            return new Result(200, "添加成功");
        } else {
            return new Result(400, "该连锁名已存在,请更换连锁名");
        }

    }

    @Override
    public Result updateTeam(AddTeamReq req) {
        Team team = teamDao.findOne(req.getId());
        if (team != null && !StringUtils.equals(team.getName(), req.getName())) {
            team.setName(req.getName());
            teamDao.save(team);
            return new Result(200, "修改成功");
        } else if (team == null) {
            return new Result(400, "该连锁不存在,请核实");
        } else {
            return new Result(400, "该连锁名已存在,请重新填写");
        }
    }

    @Override
    public List<Team> findAllTeam() {
        List<Team> team =teamDao.findAll();
        return team;

    }
}
